package com.water.note.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.water.note.common.Result;
import com.water.note.entity.Achievement;

import com.water.note.service.AchievementService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 成果记录控制器
 */
@RestController
@RequestMapping("/achievement")
@RequiredArgsConstructor
public class AchievementController {

    private final AchievementService achievementService;


    /**
     * 分页查询成果记录
     *
     * @param page       页码
     * @param pageSize   每页大小
     * @param categoryId 分类ID
     * @param keyword    关键字
     * @return 分页结果
     */
    @GetMapping("/page")
    public Result<Page<Achievement>> page(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Long categoryId,
            @RequestParam(required = false) String keyword) {

        Long userId = StpUtil.getLoginIdAsLong();
        Page<Achievement> pageParam = new Page<>(page, pageSize);
        Page<Achievement> pageResult = achievementService.page(pageParam, userId, categoryId, keyword);
        return Result.success(pageResult);
    }

    /**
     * 根据ID查询成果记录详情
     *
     * @param id 成果ID
     * @return 成果详情
     */
    @GetMapping("/{id}")
    public Result<Achievement> getById(@PathVariable Long id) {
        Achievement achievement = achievementService.getDetailById(id);
        return Result.success(achievement);
    }

    /**
     * 新增成果记录
     *
     * @param params 成果信息及图片
     * @return 操作结果
     */
    @PostMapping
    public Result<Achievement> save(@RequestBody Map<String, Object> params) {
        Achievement achievement = new Achievement();
        // 设置成果信息
        achievement.setTitle((String) params.get("title"));
        achievement.setDescription((String) params.get("description"));
        achievement.setCategoryId(Long.valueOf(params.get("categoryId").toString()));
        achievement.setTags((String) params.get("tags"));
        achievement.setIsPublic(Integer.valueOf(params.get("isPublic").toString()));
        

        achievement.setUserId(StpUtil.getLoginIdAsLong());
        
        // 获取图片列表
        @SuppressWarnings("unchecked")
        List<String> imageUrls = (List<String>) params.get("imageUrls");
        
        // 保存成果及图片
        achievementService.saveWithImages(achievement, imageUrls);
        
        return Result.success(achievement);
    }

    /**
     * 更新成果记录
     *
     * @param params 成果信息及图片
     * @return 操作结果
     */
    @PutMapping
    public Result<Boolean> update(@RequestBody Map<String, Object> params) {
        Achievement achievement = new Achievement();
        // 设置成果信息
        achievement.setId(Long.valueOf(params.get("id").toString()));
        achievement.setTitle((String) params.get("title"));
        achievement.setDescription((String) params.get("description"));
        achievement.setCategoryId(Long.valueOf(params.get("categoryId").toString()));
        achievement.setTags((String) params.get("tags"));
        achievement.setIsPublic(Integer.valueOf(params.get("isPublic").toString()));
        
        // 获取图片列表
        @SuppressWarnings("unchecked")
        List<String> imageUrls = (List<String>) params.get("imageUrls");
        
        // 更新成果及图片
        boolean result = achievementService.updateWithImages(achievement, imageUrls);
        
        return Result.success(result);
    }

    /**
     * 删除成果记录
     *
     * @param id 成果ID
     * @return 操作结果
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {

        // 删除成果记录
        boolean result = achievementService.removeById(id);
        return Result.success(result);
    }


} 